package com.gjs.demo002;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toolbar;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.gjs.demo002.adapter.MyListAdapter;
import com.gjs.demo002.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ListViewActivity extends AppCompatActivity {
    private ListView listView;

    List<UserInfo> userList = new ArrayList<>();

    int num;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        num = bundle.getInt("num");
        Log.e("guang","num = " + num);


        /** 以下是生成列表 **/
        listView = findViewById(R.id.activity_listview_lv);

        initData(); // 初始化数据

        // new一个adapter
        MyListAdapter myListAdapter = new MyListAdapter(this,userList);
        listView.setAdapter(myListAdapter);

        // 列表每一项的点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.e("guang","点击的是：" + i);
            }
        });

        /** 以下是返回上一页 **/
        Toolbar toolbar = findViewById(R.id.activity_list_toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


    }

    // 初始化数据
    private void initData(){
        Random r = new Random();
        for (int i = 0; i < num; i++){
            UserInfo userInfo = new UserInfo();
            userInfo.setUserName("张三" + (i+1));
            userInfo.setAge(r.nextInt(15)+6);
            userInfo.setSex(r.nextInt(10)%2 == 0 ? "男" : "女");
            userInfo.setGrade(5);
            userList.add(userInfo);
        }
    }


}
